﻿using System;
using DataTier;

namespace BusinessTier
{
    public class DataManipulation
    {
        private DataTier.DataTier dt;

        public DataManipulation()
        {
            dt = new DataTier.DataTier();
        }

        /// <summary>
        /// Create a new user
        /// </summary>
        /// <param name="newUserName">new user name</param>
        /// <param name="newUserPass">new password</param>
        /// <param name="email">new email address</param>
        /// <returns>"200" successed, otherwise failed</returns>
        /// <remarks></remarks>
        public string CreateUser(string newUserName, string newUserPass, string email)
        {
            return dt.CreateUser(newUserName, newUserPass, email);
        }

        /// <summary>
        /// Check if an username is already exist in the database
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>true: exist,false: not exist</returns>
        /// <remarks></remarks>
        public bool IsExist(string userName)
        {
            return (dt.IsExist(userName));
        }

        /// <summary>
        /// Check if the supplied username and password match the database record
        /// </summary>
        /// <param name="userName">username</param>
        /// <param name="pass">password</param>
        /// <returns>true: match, false: not match</returns>
        /// <remarks></remarks>
        public object LoginCheck(string userName, string pass)
        {
            return dt.LoginCheck(userName, pass);
        }

        /// <summary>
        /// Change password for a loged in user
        /// </summary>
        /// <param name="userName">username</param>
        /// <param name="newPass">new passowrd</param>
        /// <returns>'200' successful, otherwise failed</returns>
        /// <remarks></remarks>
        public object ChangePassword(string userName, string newPass)
        {
            return dt.ChangePassword(userName, newPass);
        }

        /// <summary>
        /// Upload an avatar to server and set it to user
        /// </summary>
        /// <param name="f">image file bytes</param>
        /// <param name="userName">user</param>
        /// <returns>'200' successful, otherwise failed</returns>
        /// <remarks></remarks>
        public object ChangeAvatar(byte[] f, string userName)
        {
            return dt.ChangeAvatar(f, userName);
        }

        /// <summary>
        /// Get the default avatar image address 
        /// </summary>
        /// <returns>image url</returns>
        /// <remarks></remarks>
        public object GetDefaultAvatar()
        {
            return dt.GetDefaultAvatar();
        }

        /// <summary>
        /// Load avatar image url for user
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>image url</returns>
        /// <remarks></remarks>
        public object LoadAvatar(string userName)
        {
            return dt.LoadAvatar(userName);
        }

        /// <summary>
        /// Change the best record for user
        /// </summary>
        /// <param name="time">time to finish maze</param>
        /// <param name="userName"></param>
        /// <returns>'200' success, otherwise failed</returns>
        /// <remarks></remarks>
        public object NewBestRecord(string time, string userName)
        {
            return dt.NewBestRecord(time, userName);
        }

        /// <summary>
        /// Get the best record for a user
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public object GetBestRecord(string userName)
        {
            return dt.GetBestRecord(userName);
        }

        /// <summary>
        /// Create a new game in the Game table
        /// </summary>
        /// <param name="userName">users that in this game</param>
        /// <param name="game_id">game guid</param>
        /// <param name="mazeData">maze json string</param>
        /// <returns>'200' sucess</returns>
        /// <remarks></remarks>
        public string CreateGame(string userName, string game_id, string mazeData)
        {
            return dt.CreateGame(userName, game_id, mazeData);
        }

        /// <summary>
        /// Apend a new username in a game record's user column
        /// </summary>
        /// <param name="newPlayer">new player's name</param>
        /// <param name="game_id">game id</param>
        /// <returns>'200' success</returns>
        /// <remarks></remarks>
        public string AddNewPlayerToGame(string newPlayer, string game_id)
        {
            return dt.AddNewPlayerToGame(newPlayer, game_id);
        }

        /// <summary>
        /// Delete a game from game table
        /// </summary>
        /// <param name="gameID">game id</param>
        /// <returns>'200' success</returns>
        /// <remarks></remarks>
        public string DeleteAFinishedGame(string gameID)
        {
            return dt.DeleteAFinishedGame(gameID);
        }

        /// <summary>
        /// Get all players' name  that are in particularly game
        /// </summary>
        /// <param name="gameID">current game's id</param>
        /// <returns>an string array that contain every player's name</returns>
        /// <remarks></remarks>
        public string[] GetAllPlayersInGame(string gameID)
        {
            return dt.GetAllPlayersInGame(gameID);
        }

        /// <summary>
        /// Add the game id to user table's Game_ID column
        /// </summary>
        /// <param name="user">user name</param>
        /// <param name="gameID">game id</param>
        /// <returns>'200' success</returns>
        /// <remarks></remarks>
        public string AddUserToGame(string user, string gameID)
        {
            return dt.AddUserToGame(user, gameID);
        }

        /// <summary>
        /// Return an string array that contain all information associated with this user
        /// </summary>
        /// <param name="username"></param>
        /// <returns>Return array contains element in following order<br/>
        /// UserName,UserAvatar,UserEmail,User_BestRecord,Game_ID
        /// </returns>
        /// <remarks></remarks>
        public string[] GetAllUserData(string username)
        {
            dynamic res = dt.GetAllUserData(username);
            return res.ToArray();
        }

        /// <summary>
        /// Return X amount of user data that is sorted from lowest best record to highest
        /// </summary>
        /// <param name="user_num"></param>
        /// <returns>return array contains element in folowing order<br/>
        /// "User1","1","User2","2"......
        /// </returns>
        /// <remarks></remarks>
        public TopRecord[] GetTopXUsers(Int16 user_num)
        {
            return GetTopXUsers(user_num);
        }

        /// <summary>
        /// Update user position data based on user id supplied
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="pos"></param>
        /// <param name="dir"></param>
        /// <param name="gameID"></param>
        /// <returns>'200' success</returns>
        /// <remarks></remarks>
        public string StorePosOfPlayer(Int16 playerId, string pos, string dir, string gameID)
        {
            return dt.StorePosOfPlayer(playerId, pos, dir, gameID);
        }

        /// <summary>
        /// Get user position and directin data based on id supplied
        /// </summary>
        /// <param name="gameid"></param>
        /// <param name="playerID"></param>
        /// <returns>["position data","direction data"]</returns>
        /// <remarks></remarks>
        public string[] GetPlayerPos(string gameid, Int16 playerID)
        {
            return dt.GetPlayerPos(gameid, playerID);
        }
    }
}